
import com.eep.security.*;
import com.eep.security.Session.SecurityStatus;

import com.eep.inventory.InventoryItem;
import com.eep.inventory.InventoryService;
import com.eep.inventory.InventoryServiceDelegate;
import com.eep.inventory.InventoryType;

import java.util.*;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DistOrderApp.java
 *
 * Created on Feb 20, 2012, 4:49:53 PM
 * @author Michael Azzopardi (mazzopar@andrew.cmu.edu)
 */

/**
 *
 * @author michael
 */
public class DistOrderApp extends javax.swing.JFrame {

    private boolean authenticationCancelled;
    private boolean authenticationFailed;
    public Session session;
    private SecurityService securityService = new SecurityServiceImpl();

    /** Creates new form DistOrderApp */
    public DistOrderApp() {
        session = new Session();
        session.setApplicationId(Session.Applications.Orders);
        session.setStatus(SecurityStatus.unkownUser);

        initComponents();
        //Generally not a good idea to launch a second window from within the
        //constructor of the first.  There's no way to kill this window since
        //it hasn't finished constructing yet.
        //As a work around, I added a boolean check, and when the main window is
        //opened, if authenticaton has failed, it will quickly dispose of the
        //main window and exit.
        loginDialog.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginDialog = new javax.swing.JDialog();
        usernameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        usernameField = new javax.swing.JTextField();
        passwordField = new javax.swing.JPasswordField();
        loginButton = new javax.swing.JButton();
        errorLabel = new java.awt.Label();
        instructionLabel = new javax.swing.JLabel();
        cancelButton = new javax.swing.JButton();
        headerPanel = new javax.swing.JPanel();
        companyNameLabel = new javax.swing.JLabel();
        appNameLabel = new javax.swing.JLabel();
        orderInfoPanel = new javax.swing.JPanel();
        firstNameLabel = new javax.swing.JLabel();
        firstNameField = new javax.swing.JTextField();
        lastNameLabel = new javax.swing.JLabel();
        lastNameField = new javax.swing.JTextField();
        addressLabel = new javax.swing.JLabel();
        addressScrollPane = new javax.swing.JScrollPane();
        addressTextArea = new javax.swing.JTextArea();
        jLabel9 = new javax.swing.JLabel();
        phoneTextField = new javax.swing.JTextField();
        customerInfoLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        orderContentPanel = new javax.swing.JPanel();
        inventoryTabbedPane = new javax.swing.JTabbedPane();
        treesPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        treesTextArea = new javax.swing.JTextArea();
        seedsPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        seedsTextArea = new javax.swing.JTextArea();
        shrubsPanel = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        shrubsTextArea = new javax.swing.JTextArea();
        addItemButton = new javax.swing.JButton();
        submitOrderButton = new javax.swing.JButton();
        orderListPanel = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        inventoryLabel = new javax.swing.JLabel();
        orderListLabel = new javax.swing.JLabel();
        orderNumberLabel = new javax.swing.JLabel();
        orderNumberField = new javax.swing.JTextField();
        totalField = new javax.swing.JTextField();
        removeItemButton = new javax.swing.JButton();
        totalLabel = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        exitMenuItem = new javax.swing.JMenuItem();

        loginDialog.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        loginDialog.setTitle("EEP Login");
        loginDialog.setIconImage(null);
        loginDialog.setMinimumSize(new java.awt.Dimension(400, 300));
        loginDialog.setModal(true);
        loginDialog.setName("Login"); // NOI18N
        loginDialog.setResizable(false);

        usernameLabel.setText("Username");

        passwordLabel.setText("Password");

        usernameField.setToolTipText("");

        loginButton.setText("Login");
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginAction(evt);
            }
        });

        errorLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        errorLabel.setForeground(new java.awt.Color(255, 0, 0));
        errorLabel.setName("Errors"); // NOI18N

        instructionLabel.setText("Please enter your username and password to login to the system");

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout loginDialogLayout = new javax.swing.GroupLayout(loginDialog.getContentPane());
        loginDialog.getContentPane().setLayout(loginDialogLayout);
        loginDialogLayout.setHorizontalGroup(
            loginDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginDialogLayout.createSequentialGroup()
                .addGap(128, 128, 128)
                .addComponent(cancelButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loginButton)
                .addContainerGap(144, Short.MAX_VALUE))
            .addGroup(loginDialogLayout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addGroup(loginDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(usernameLabel)
                    .addComponent(passwordLabel))
                .addGap(18, 18, 18)
                .addGroup(loginDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(usernameField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(passwordField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(126, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginDialogLayout.createSequentialGroup()
                .addContainerGap(49, Short.MAX_VALUE)
                .addGroup(loginDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(errorLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(instructionLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(39, 39, 39))
        );
        loginDialogLayout.setVerticalGroup(
            loginDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginDialogLayout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(instructionLabel)
                .addGap(18, 18, 18)
                .addGroup(loginDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(loginDialogLayout.createSequentialGroup()
                        .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(6, 6, 6))
                    .addGroup(loginDialogLayout.createSequentialGroup()
                        .addComponent(usernameLabel)
                        .addGap(18, 18, 18)
                        .addComponent(passwordLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(errorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginButton)
                    .addComponent(cancelButton))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Exton Exotic Plants - Distributed Orders Application");
        setBackground(new java.awt.Color(255, 255, 255));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        headerPanel.setBackground(new java.awt.Color(255, 255, 255));

        companyNameLabel.setFont(new java.awt.Font("Times New Roman", 3, 36));
        companyNameLabel.setForeground(new java.awt.Color(0, 153, 0));
        companyNameLabel.setText("Exton Exotic Plants");

        appNameLabel.setFont(new java.awt.Font("Arial", 0, 18));
        appNameLabel.setForeground(new java.awt.Color(102, 51, 0));
        appNameLabel.setText("Distributed Orders Application");

        javax.swing.GroupLayout headerPanelLayout = new javax.swing.GroupLayout(headerPanel);
        headerPanel.setLayout(headerPanelLayout);
        headerPanelLayout.setHorizontalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(companyNameLabel)
                    .addComponent(appNameLabel))
                .addContainerGap(316, Short.MAX_VALUE))
        );
        headerPanelLayout.setVerticalGroup(
            headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(headerPanelLayout.createSequentialGroup()
                .addComponent(companyNameLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(appNameLabel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        orderInfoPanel.setBackground(new java.awt.Color(255, 255, 255));

        firstNameLabel.setText("First Name");

        lastNameLabel.setText("Last Name");

        lastNameField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lastNameFieldActionPerformed(evt);
            }
        });

        addressLabel.setText("Address");

        addressTextArea.setColumns(20);
        addressTextArea.setRows(5);
        addressScrollPane.setViewportView(addressTextArea);

        jLabel9.setText("Phone #");

        phoneTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                phoneTextFieldActionPerformed(evt);
            }
        });

        customerInfoLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        customerInfoLabel.setText("Customer Information");

        javax.swing.GroupLayout orderInfoPanelLayout = new javax.swing.GroupLayout(orderInfoPanel);
        orderInfoPanel.setLayout(orderInfoPanelLayout);
        orderInfoPanelLayout.setHorizontalGroup(
            orderInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(orderInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(orderInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(orderInfoPanelLayout.createSequentialGroup()
                        .addGroup(orderInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(firstNameLabel)
                            .addComponent(jLabel9)
                            .addComponent(addressLabel))
                        .addGap(18, 18, 18)
                        .addGroup(orderInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(addressScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                            .addGroup(orderInfoPanelLayout.createSequentialGroup()
                                .addComponent(phoneTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                                .addGap(203, 203, 203))
                            .addGroup(orderInfoPanelLayout.createSequentialGroup()
                                .addComponent(firstNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lastNameLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lastNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)))
                        .addGap(183, 183, 183))
                    .addGroup(orderInfoPanelLayout.createSequentialGroup()
                        .addComponent(customerInfoLabel)
                        .addContainerGap(477, Short.MAX_VALUE))))
        );
        orderInfoPanelLayout.setVerticalGroup(
            orderInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(orderInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(customerInfoLabel)
                .addGap(18, 18, 18)
                .addGroup(orderInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstNameLabel)
                    .addComponent(firstNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(orderInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(addressLabel)
                    .addGroup(orderInfoPanelLayout.createSequentialGroup()
                        .addComponent(addressScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(orderInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(phoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))))
                .addContainerGap(34, Short.MAX_VALUE))
        );

        orderContentPanel.setBackground(new java.awt.Color(255, 255, 255));

        treesPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                treesPanelComponentShown(evt);
            }
        });

        treesTextArea.setColumns(20);
        treesTextArea.setRows(5);
        treesTextArea.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                treesTextAreaComponentShown(evt);
            }
        });
        jScrollPane1.setViewportView(treesTextArea);

        javax.swing.GroupLayout treesPanelLayout = new javax.swing.GroupLayout(treesPanel);
        treesPanel.setLayout(treesPanelLayout);
        treesPanelLayout.setHorizontalGroup(
            treesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
        );
        treesPanelLayout.setVerticalGroup(
            treesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
        );

        inventoryTabbedPane.addTab("Trees", treesPanel);

        seedsPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                seedsPanelComponentShown(evt);
            }
        });

        seedsTextArea.setColumns(20);
        seedsTextArea.setRows(5);
        seedsTextArea.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                seedsTextAreaComponentShown(evt);
            }
        });
        jScrollPane2.setViewportView(seedsTextArea);

        javax.swing.GroupLayout seedsPanelLayout = new javax.swing.GroupLayout(seedsPanel);
        seedsPanel.setLayout(seedsPanelLayout);
        seedsPanelLayout.setHorizontalGroup(
            seedsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
        );
        seedsPanelLayout.setVerticalGroup(
            seedsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
        );

        inventoryTabbedPane.addTab("Seeds", seedsPanel);

        shrubsPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                shrubsPanelComponentShown(evt);
            }
        });

        shrubsTextArea.setColumns(20);
        shrubsTextArea.setRows(5);
        shrubsTextArea.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                shrubsTextAreaComponentShown(evt);
            }
        });
        jScrollPane3.setViewportView(shrubsTextArea);

        javax.swing.GroupLayout shrubsPanelLayout = new javax.swing.GroupLayout(shrubsPanel);
        shrubsPanel.setLayout(shrubsPanelLayout);
        shrubsPanelLayout.setHorizontalGroup(
            shrubsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 255, Short.MAX_VALUE)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
        );
        shrubsPanelLayout.setVerticalGroup(
            shrubsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 196, Short.MAX_VALUE)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
        );

        inventoryTabbedPane.addTab("Shrubs", shrubsPanel);

        addItemButton.setText("Add item");
        addItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemButtonActionPerformed(evt);
            }
        });

        submitOrderButton.setText("Submit order");

        orderListPanel.setBackground(new java.awt.Color(255, 255, 255));
        orderListPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jScrollPane4.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setText("Steve to add code for populating and submitting order");
        jScrollPane4.setViewportView(jTextArea1);

        javax.swing.GroupLayout orderListPanelLayout = new javax.swing.GroupLayout(orderListPanel);
        orderListPanel.setLayout(orderListPanelLayout);
        orderListPanelLayout.setHorizontalGroup(
            orderListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
        );
        orderListPanelLayout.setVerticalGroup(
            orderListPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
        );

        inventoryLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        inventoryLabel.setText("Inventory");

        orderListLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        orderListLabel.setText("Order List");

        orderNumberLabel.setText("Order #");

        orderNumberField.setEditable(false);

        totalField.setEditable(false);

        removeItemButton.setText("Remove Item");

        totalLabel.setText("Total");

        javax.swing.GroupLayout orderContentPanelLayout = new javax.swing.GroupLayout(orderContentPanel);
        orderContentPanel.setLayout(orderContentPanelLayout);
        orderContentPanelLayout.setHorizontalGroup(
            orderContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(orderContentPanelLayout.createSequentialGroup()
                .addGroup(orderContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(orderContentPanelLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(addItemButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, orderContentPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(inventoryTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(orderContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(orderContentPanelLayout.createSequentialGroup()
                        .addComponent(submitOrderButton)
                        .addGap(296, 296, 296))
                    .addGroup(orderContentPanelLayout.createSequentialGroup()
                        .addGroup(orderContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(orderContentPanelLayout.createSequentialGroup()
                                .addComponent(totalLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(totalField, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(removeItemButton))
                            .addComponent(orderListLabel, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, orderContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, orderContentPanelLayout.createSequentialGroup()
                                    .addComponent(orderNumberLabel)
                                    .addGap(18, 18, 18)
                                    .addComponent(orderNumberField))
                                .addComponent(orderListPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(70, 70, 70))))
            .addGroup(orderContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(orderContentPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(inventoryLabel)
                    .addContainerGap(594, Short.MAX_VALUE)))
        );
        orderContentPanelLayout.setVerticalGroup(
            orderContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, orderContentPanelLayout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(orderListLabel)
                .addGap(10, 10, 10)
                .addGroup(orderContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(inventoryTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(orderContentPanelLayout.createSequentialGroup()
                        .addGroup(orderContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(orderNumberLabel)
                            .addComponent(orderNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(orderListPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
                .addGroup(orderContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addItemButton)
                    .addComponent(removeItemButton)
                    .addComponent(totalLabel)
                    .addComponent(totalField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(submitOrderButton)
                .addContainerGap())
            .addGroup(orderContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(orderContentPanelLayout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addComponent(inventoryLabel)
                    .addContainerGap(338, Short.MAX_VALUE)))
        );

        inventoryTabbedPane.getAccessibleContext().setAccessibleName("Trees");

        jMenu1.setText("File");

        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        jMenu1.add(exitMenuItem);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(headerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 604, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(orderInfoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(orderContentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 624, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(headerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(orderInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(orderContentPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        // TODO add your handling code here:
        System.out.println("Exit application");
        exit();
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void loginAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginAction
        // TODO add your handling code here:
        // Populate session object
        session.setUsername(usernameField.getText());
        System.out.println("Username is :"+session.getUsername());
        if (session.getUsername().length() > 0) {
            session.setPassword(passwordField.getText());
            System.out.println("Password is :"+session.getPassword());
            if (session.getPassword().length() > 0) {
                // SecurityService to authenticate
                //
                //
                //
                session.setStatus(securityService.authenticate(session.getUsername(), session.getPassword()));
                //
                //
                //
                System.out.println("Status is :"+session.getStatus());
                if (session.getStatus() == SecurityStatus.authenticated) {
                    // Security service to authorize

                    // if authorization works
                    if (securityService.authorize(session)) {
                      loginDialog.dispose();
                    } else {
                      errorLabel.setText("You are not authorized for this application");
                    }

                } else {
                    // TODO: Write message
                    errorLabel.setText("Invalid login");
                }
            } else {
                errorLabel.setText("Please enter password");
            }
        } else {
            errorLabel.setText("Please enter username");
        }
        

    }//GEN-LAST:event_loginAction

    private void lastNameFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lastNameFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_lastNameFieldActionPerformed

    private void phoneTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_phoneTextFieldActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_phoneTextFieldActionPerformed

    private void addItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_addItemButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        System.out.println(this.getClass());
        this.authenticationCancelled = true;
        exit();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        if (authenticationFailed || authenticationCancelled) {
            dispose();
        }
    }//GEN-LAST:event_formWindowOpened

    private void treesPanelComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_treesPanelComponentShown
        // TODO add your handling code here:
        treesTextArea.setText("");

        // Uses the Inventory Service Business Delegate to load the
        // inventory item data for trees.
        
        InventoryService invService = new InventoryServiceDelegate().getInventoryService();
        List<InventoryItem> iList = invService.getInventory(InventoryType.Trees);
        for (InventoryItem i : iList) {
            treesTextArea.append(i + "\n");
        }
        
    }//GEN-LAST:event_treesPanelComponentShown

    private void treesTextAreaComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_treesTextAreaComponentShown
        // TODO add your handling code here:
    }//GEN-LAST:event_treesTextAreaComponentShown

    private void seedsTextAreaComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_seedsTextAreaComponentShown
        // TODO add your handling code here:
    }//GEN-LAST:event_seedsTextAreaComponentShown

    private void shrubsTextAreaComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_shrubsTextAreaComponentShown
        // TODO add your handling code here:
    }//GEN-LAST:event_shrubsTextAreaComponentShown

    private void seedsPanelComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_seedsPanelComponentShown
        // TODO add your handling code here:
       seedsTextArea.setText("");

        // Uses the Inventory Service Business Delegate to load the
        // inventory item data for trees.

        InventoryService invService = new InventoryServiceDelegate().getInventoryService();
        List<InventoryItem> iList = invService.getInventory(InventoryType.Seeds);
        for (InventoryItem i : iList) {
            seedsTextArea.append(i + "\n");
        }
    }//GEN-LAST:event_seedsPanelComponentShown

    private void shrubsPanelComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_shrubsPanelComponentShown
        // TODO add your handling code here:
                // TODO add your handling code here:
        shrubsTextArea.setText("");

        // Uses the Inventory Service Business Delegate to load the
        // inventory item data for trees.

        InventoryService invService = new InventoryServiceDelegate().getInventoryService();
        List<InventoryItem> iList = invService.getInventory(InventoryType.Shrubs);
        for (InventoryItem i : iList) {
            shrubsTextArea.append(i + "\n");
        }
    }//GEN-LAST:event_shrubsPanelComponentShown

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DistOrderApp().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addItemButton;
    private javax.swing.JLabel addressLabel;
    private javax.swing.JScrollPane addressScrollPane;
    private javax.swing.JTextArea addressTextArea;
    private javax.swing.JLabel appNameLabel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel companyNameLabel;
    private javax.swing.JLabel customerInfoLabel;
    private java.awt.Label errorLabel;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JTextField firstNameField;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JPanel headerPanel;
    private javax.swing.JLabel instructionLabel;
    private javax.swing.JLabel inventoryLabel;
    private javax.swing.JTabbedPane inventoryTabbedPane;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField lastNameField;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JButton loginButton;
    private javax.swing.JDialog loginDialog;
    private javax.swing.JPanel orderContentPanel;
    private javax.swing.JPanel orderInfoPanel;
    private javax.swing.JLabel orderListLabel;
    private javax.swing.JPanel orderListPanel;
    private javax.swing.JTextField orderNumberField;
    private javax.swing.JLabel orderNumberLabel;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JTextField phoneTextField;
    private javax.swing.JButton removeItemButton;
    private javax.swing.JPanel seedsPanel;
    private javax.swing.JTextArea seedsTextArea;
    private javax.swing.JPanel shrubsPanel;
    private javax.swing.JTextArea shrubsTextArea;
    private javax.swing.JButton submitOrderButton;
    private javax.swing.JTextField totalField;
    private javax.swing.JLabel totalLabel;
    private javax.swing.JPanel treesPanel;
    private javax.swing.JTextArea treesTextArea;
    private javax.swing.JTextField usernameField;
    private javax.swing.JLabel usernameLabel;
    // End of variables declaration//GEN-END:variables

    /**
     * Exit application
     */
    public void exit() {
       System.out.println("Exiting application");
       loginDialog.dispose();
       this.dispose();
    }
}
